package fr.labri.macchiato.client.preferences;

import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.layout.VLayout;

import fr.labri.macchiato.framework.business.model.preferences.UserPreferences;

/**
 * Widget that asks to the user if he wants Macchiato to save his preferences
 * into the cache or not.
 * 
 * @author Michelle AVOMO
 * 
 */
class SaveInfosWidget extends VLayout implements PreferencesWidget {

	private static final String SAVE = "Préférences de sauvegarde des paramètres";
	private static final String DEFAULT_VALUE = "Voulez-vous que vos paramètres soient sauvegardés de façon permanente?";
	private static final String NO_VALUE = "Non";
	private static final String YES_VALUE = "Oui";
	private static final String NAME = "saveConfig";
	private static final String WIDGET_TITLE = "Options de sauvegarde";
	private DynamicForm saveInfoForm;
	private RadioGroupItem saveInfosRadioGroupItem;

	/**
	 * Create the widget
	 */
	public SaveInfosWidget() {

		HeaderItem headerForm = new HeaderItem();
		headerForm.setDefaultValue(DEFAULT_VALUE);

		saveInfoForm = new DynamicForm();

		saveInfoForm.setTitle(SAVE);
		saveInfoForm.setWidth(200);
		saveInfoForm.setHeight(50);

		// To pick the prefered hours
		saveInfosRadioGroupItem = new RadioGroupItem();
		saveInfosRadioGroupItem.setName(NAME);
		saveInfosRadioGroupItem.setRequired(true);
		saveInfosRadioGroupItem.setVertical(false);
		saveInfosRadioGroupItem.setValueMap(YES_VALUE, NO_VALUE);
		saveInfosRadioGroupItem.setRedrawOnChange(true);
		saveInfosRadioGroupItem.setWidth(300);
		saveInfosRadioGroupItem
				.setTitle("Plutôt");
		saveInfosRadioGroupItem.setDefaultValue(YES_VALUE);

		saveInfoForm.setFields(headerForm, saveInfosRadioGroupItem);
		addMember(saveInfoForm);

	}

	/**
	 * updates the user's preference when it comes to decide if the application
	 * should either save the values or not
	 */
	@Override
	public void collectUserPreferences(UserPreferences preference) {
		preference.setUseLocalStorage(saveInfosRadioGroupItem
				.getValueAsString().equals(YES_VALUE));

	}

	/**
	 * Gets the infos from the cache and display user's answer for saving infos
	 * permanently or not
	 */
	@Override
	public void restoreUserPreferences(UserPreferences preferences) {
		// saveInfo (y/n)
		if (preferences.getUseLocalStorage())
			saveInfosRadioGroupItem.setValue(YES_VALUE);
		else
			saveInfosRadioGroupItem.setValue(NO_VALUE);
	}

	/**
	 * Submits the form for the willing to save infos permanently to validation
	 */
	@Override
	public Boolean isValid() {
		return saveInfoForm.validate();
	}

	/**
	 * Adds the widget 'saveInfo' to the current window
	 */
	@Override
	public void addToParent(Window preference) {
		preference.addItem(this);

	}
	
	@Override
	public String getTitle(){
		return WIDGET_TITLE;
	}
}
